package com.acce.system.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.acce.base.exception.ApplicationException;
import com.acce.base.pojo.ArgsPojo;
import com.acce.base.pojo.ReturnCode;
import com.acce.base.pojo.ReturnPojo;
import com.acce.system.config.ReturnCodes;
import com.acce.system.dao.ILogDao;
import com.acce.system.dao.ILoginLogDao;
import com.acce.system.dao.IReturnCodeDao;
import com.acce.system.pojo.LoginLog;
import com.acce.system.service.ILogService;
import com.alibaba.fastjson.JSONObject;

@Service("logService")
public class LogServiceImpl implements ILogService {
	
	protected Logger logger = Logger.getLogger(this.getClass());
	
	@Resource	
	private ILogDao logDao;
	
	@Resource
	private ILoginLogDao loginLogDao;
	@Resource
	private IReturnCodeDao returnCodeDao;
	
	public boolean insertLog(ArgsPojo args) throws ApplicationException{
//		String currTime = Utils.getCurrTime();
//		LoginLog log = new LoginLog();
//		log.setLoginId(Utils.makeLoginId());
//		log.setAtUserId(args.getAtUserId());
//		log.setLogType(args.getLoginLogType());
//		log.setRemarks(args.getRemarks());
//		log.setLogTime(currTime);
//		log.setLoginDevice(args.getDevice());
//		log.setLoginIp(args.getIpaddr());
//		log.setLogMac(args.getMac());
//		log.setEditUser(args.getEditUser());
//		log.setEditTime(args.getEditTime());
//		log.setUpdateUser(args.getAtUserId());
//		log.setUpdateTime(currTime);
//		return logDao.insertLog(log) > 0 ? true : false;
		return true;
	}

	public ReturnPojo loadToken(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		String business = args.getBusiness();
		LoginLog login = new LoginLog();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		String atUserId = jb.getString("atUserId");
		if(StringUtils.isEmpty(atUserId) ){
			logger.info("nsy-auth::loadToken:: missing args: [atUserId]");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		logger.info("nsy-auth::loadToken:: atUserId::" + atUserId);
		login.setAtUserId(atUserId);
		login = loginLogDao.loadToken(login);
		if(null != login && "1".equals(login.getLogType())){ //判断是否是登录
			logger.info("nsy-auth::loadToken:: logined");
			String logIp = login.getLogIp() == null ? "" : login.getLogIp();
			String logMac = login.getLogMac() == null ? "" : login.getLogMac();
			String logDevice = login.getLogDevice() == null ? "" : login.getLogDevice();
			//判断是手机端还是pc微信端
			if("pc".equals(logDevice.toLowerCase()) || "wx".equals(logDevice.toLowerCase())){
				//pc微信端只判断IP
				logger.info("nsy-auth::loadToken:: pc or wx");
				if(args.getIpaddr().equals(logIp)){
					logger.info("nsy-auth::loadToken:: one user OK");
					rp.setResult(login);
				}else{
					logger.info("nsy-auth::loadToken:: other or net change");
					ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.OTHER_DEVICE_OR_NET_CHANGE.getCode());
					rp.setReturnCode(rc.getCode());
					rp.setReturnMsg(rc.getMessage());
				}
			}else{
				logger.info("nsy-auth::loadToken:: device");
				//如果设备
				if(args.getMac().toLowerCase().equals(logMac.toLowerCase()) 
						&& args.getDevice().toLowerCase().equals(logDevice.toLowerCase())){
					if(args.getIpaddr().equals(logIp)){
						logger.info("nsy-auth::loadToken:: one user OK");
						rp.setResult(login);
					}else{ //网络变化
						logger.info("nsy-auth::loadToken:: ip change by device");
						ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.NET_CHANGE.getCode());
						rp.setReturnCode(rc.getCode());
						rp.setReturnMsg(rc.getMessage());
					}
				}else if(args.getIpaddr().equals(logIp) && args.getMac().toLowerCase().equals(logMac.toLowerCase()) 
						&& args.getDevice().toLowerCase().equals(logDevice.toLowerCase())){
					logger.info("nsy-auth::loadToken:: one user OK");
					rp.setResult(login);
				}else{ //其他设备登录
					logger.info("nsy-auth::loadToken:: other device by device");
					ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.OTHER_DEVICE_LOGIN.getCode());
					rp.setReturnCode(rc.getCode());
					rp.setReturnMsg(rc.getMessage());
				}
			}
		}
		rp.setResult(login);
		//临时去除设备冲突验证
//		if(null != login && "1".equals(login.getLogType())){ //判断是否是登录
//			String logIp = login.getLogIp() == null ? "" : login.getLogIp();
//			String logMac = login.getLogMac() == null ? "" : login.getLogMac();
//			String logDevice = login.getLogDevice() == null ? "" : login.getLogDevice();
//			//判断是手机端还是pc微信端
//			if("pc".equals(logDevice) || "wx".equals(logDevice)){
//				//pc微信端只判断IP
//				if(args.getIpaddr().equals(logIp)){
//					rp.setResult(login);
//				}else{
//					ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.OTHER_DEVICE_LOGIN.getCode());
//					rp.setReturnCode(rc.getCode());
//					rp.setReturnMsg(rc.getMessage());
//				}
//			}else{
//				//如果设备
//				if(args.getMac().toLowerCase().equals(logMac.toLowerCase()) 
//						&& args.getDevice().toLowerCase().equals(logDevice.toLowerCase())){
//					if(args.getIpaddr().equals(logIp)){
//						rp.setResult(login);
//					}else{ //网络变化
//						ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.NET_CHANGE.getCode());
//						rp.setReturnCode(rc.getCode());
//						rp.setReturnMsg(rc.getMessage());
//					}
//				}else if(args.getIpaddr().equals(logIp) && args.getMac().toLowerCase().equals(logMac.toLowerCase()) 
//						&& args.getDevice().toLowerCase().equals(logDevice.toLowerCase())){
//					rp.setResult(login);
//				}else{ //其他设备登录
//					ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.OTHER_DEVICE_LOGIN.getCode());
//					rp.setReturnCode(rc.getCode());
//					rp.setReturnMsg(rc.getMessage());
//				}
//			}
//		}
		return rp;
	}
	
	//无验证获取token
	public ReturnPojo loadTokenNoVali(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		String business = args.getBusiness();
		LoginLog login = new LoginLog();
		logger.info("nsy-auth::loadTokenNoVali:: business::" + business);
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		String atUserId = jb.getString("atUserId");
		logger.info("nsy-auth::loadTokenNoVali:: atUserId::" + atUserId);
		if(StringUtils.isEmpty(atUserId) ){
			logger.info("nsy-auth::loadToken:: missing args: [atUserId]");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		login.setAtUserId(atUserId);
		login = loginLogDao.loadToken(login);
		logger.info("nsy-auth::loadTokenNoVali:: login::" + login);
		if(null == login){
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.NOT_LOGIN.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}else{
			rp.setResult(login);
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}
		return rp;
	}
}
